package com.ruoyi.app.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.ApiResult;
import com.ruoyi.ums.entity.Member;
import com.ruoyi.ums.service.IMemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = "test")
@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    private IMemberService memberService;
    @Autowired
    private RedisCache redisCache;

    @ApiOperation("registerMember")
    @RequestMapping(value = "/registerMember",method = RequestMethod.GET)
    public ApiResult<Void> registerMember(@RequestParam("openId") String openId){
        QueryWrapper<Member> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(Member::getOpenId,openId);
        Member member = memberService.getOne(wrapper);
        if(null == member)
            throw new ServiceException("openId错误，请重新输入");
        redisCache.setCacheObject(Constants.APP_TOKEN_KEY + openId, JSON.toJSONString(member));

        return ApiResult.success();
    }
}
